package com.testingedu.action.background;

import com.testingedu.jdbcDao.TStudyStateDao;
import com.testingedu.jdbcDao.TUserDao;
import com.testingedu.model.TStudyState;
import com.testingedu.model.TUser;

public class TStudyStateAction extends RequestContext {

	private static final long serialVersionUID = -7459638891931418045L;
	private TStudyState tStudyState;
	private TStudyStateDao tStudyStateDao = new TStudyStateDao();

	public TStudyState gettStudyState() {
		return tStudyState;
	}

	public void settStudyState(TStudyState tStudyState) {
		this.tStudyState = tStudyState;
	}

	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			int userId = Integer.parseInt(request.getParameter("userId"));
			TUser tUser = new TUserDao().info(userId);
			request.setAttribute("tUser", tUser);
			request.getRequestDispatcher("/WEB-INF/admin/jsp/studyState/add.jsp").forward(
							request, response);
		} else {
			try {
				StringBuffer msg = new StringBuffer();
				int userId = Integer.parseInt(request.getParameter("userId"));
				TUser tUser = new TUser(userId);
				tStudyState.settUser(tUser);
				int id = tStudyStateDao.add(tStudyState);
				msg.append("{\"id\":" + id + "");
				msg.append(",\"msg\":\"null\"}");
				response.getWriter().write(msg.toString());
				response.getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void _delete() throws Exception {
		tStudyStateDao.delete(tStudyState.getId());
		response.sendRedirect("/testingedu2/admin/studyState/list");
	}

	public void _update() throws Exception {
		String url = request.getParameter("url");
		if ("update".equals(url)) {
			this.info(url);
		} else {
			StringBuffer msg = new StringBuffer();
			tStudyStateDao.update(tStudyState);
			msg.append("{\"id\":" + tStudyState.getId() + "");
			msg.append(",\"msg\":\"null\"}");
			response.getWriter().write(msg.toString());
			response.getWriter().close();
		}
	}

	public void _list() throws Exception {
		String url = request.getParameter("url");
		if ("info".equals(url)) {
			this.info(url);
		} else {
			createQueryResult();
			String userIdStr = request.getParameter("userId");
			String userName = request.getParameter("userName");
			int userId = 0;
			if(userIdStr != null && userIdStr.trim().length() > 0) {
				userId = Integer.parseInt(userIdStr);
			}
			if(userName != null && userName.trim().length() > 0) {
				TUser t = new TUser();
				t.setName(userName);
				if(tStudyState == null)
					tStudyState = new TStudyState();
				tStudyState.settUser(t);
			}
			tStudyStateDao.list(queryResult, tStudyState, userId);
			request.setAttribute("queryResult", queryResult);
			request.setAttribute("userId", userId);
			request.getRequestDispatcher(
					"/WEB-INF/admin/jsp/studyState/list.jsp").forward(request,
					response);
		}
	}

	private void info(String url) throws Exception {
		tStudyState = tStudyStateDao.info(tStudyState.getId());
		request.setAttribute("tStudyState", tStudyState);
		request.getRequestDispatcher(
				"/WEB-INF/admin/jsp/studyState/" + url + ".jsp").forward(
				request, response);
	}
}
